const express=require('express')


// 引入rModel对象
const rModel = require('../db/model')

// 创建路由器对象
const router = express.Router();

// 书写注册路由
router.post('/register', async (req, res) => {
    console.log(111);
    const { username, password } = req.body;
    // 尝试创建，若无法创建就注册失败
    try {
        await rModel.create({ name: username, password });
        res.redirect('http://127.0.0.1:666/login')
    } catch (err) {
        console.log(err);
        res.send("注册失败");}
    
});
    
router.post('/login', async (req, res) => {
    console.log(1111);
    const { username, password } = req.body;
    const result = await rModel.findOne({ name: username, password });
    if (result) {
        // 有数据
        console.log('login成功');
        res.redirect('http://127.0.0.1:666/html/index.html')
    } else {
        res.send('登录失败-账号或者密码错误')
    }
})


module.exports = router;

